package com.sannu.emc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginDb
 */
public class LoginDb extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginDb() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		 response.setContentType("text/html");  
		    PrintWriter pw=response.getWriter();
		    
		    String Driver="com.mysql.jdbc.Driver";
		    String connectionURL = "jdbc:mysql://localhost:3306/Students"; 
		    Connection connection;
		    
		    try{  
		     
		      String username = request.getParameter("user"); 
		      String password = request.getParameter("pass");
		     
		      
		      Class.forName(Driver);
		      
		      
		      connection = DriverManager.getConnection(connectionURL, "root", "sannu"); 
		     
		      PreparedStatement pst = connection.prepareStatement("insert into LOGIN (username,password) values(?, ?)");
		      
		      pst.setString(1, username);
		      pst.setString(2, password);
		     
		      int i = pst.executeUpdate();  
		      if(i!=0){  
		        pw.println("<br>Login Successfull");  
		          
		  
		      }  
		      else{  
		        pw.println("Failed to login");  
		       }  
		    }  
		    catch (Exception e){  
		    	System.out.println("Exception : " + e.toString());
		    	 
		    } 
		    
			
					  
	}

}
